package NO04;

import java.util.Stack;

/*
 * 输入一个链表，从尾到头打印链表每个节点的值。
 */
public class PrintList {
	
	public static class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val){
            this.val = val;
        }
    }
	
	public static void printListFromTailToHead(ListNode node){
		//利用栈的先进后出的性质来实现链表的反向输出
		Stack<Integer> stack = new Stack();
		
		System.out.println("排序前: ");
		while(node!=null){
			System.out.print(node.val);
			stack.push(node.val);
			node = node.next;
		}
		System.out.println();
		
		System.out.println("排序后: ");
		while(!stack.isEmpty()){
			System.out.print(stack.pop());
		}
	}
	
	public static void main(String[] args) {
		ListNode root = new ListNode(1);
		root.next = new ListNode(2);
		root.next.next = new ListNode(3);
		root.next.next.next = new ListNode(4);
		root.next.next.next.next = new ListNode(5);
		
		printListFromTailToHead(root);
	}

}
